const Koa = require('koa')
const Router = require('koa-router')
const bodyParser = require('koa-bodyparser')
const edge = require('edge-js')

const app = new Koa()

app.use(bodyParser({formLimit: "50mb"}))

const cors = require('koa2-cors');

app.use(
  cors({
      origin: function(ctx) { //设置允许来自指定域名请求
          // if (ctx.url === '/print') {
          // }
          return '*'; // 允许来自所有域名请求
          // return 'http://localhost:8080'; //只允许http://localhost:8080这个域名的请求
      },
      maxAge: 5, //指定本次预检请求的有效期，单位为秒。
      credentials: true, //是否允许发送Cookie
      allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], //设置所允许的HTTP请求方法'
      allowHeaders: ['Content-Type', 'Authorization', 'Accept'], //设置服务器支持的所有头信息字段
      exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'] //设置获取其他自定义字段
  })
);

function edgeFunc(methodName) {
  return edge.func({
    assemblyFile: 'tsclibnet.dll',
    typeName: 'TSCSDK.node_usb',
    methodName
  })
}

const fontstyle = {
  'Normal': 0,
  'Italic': 1,
  'Bold': 2,
  'Bold and Italic': 3
}
const router = new Router({})
router.get('/print', ctx => {
  ctx.body = 'success'
})

router.post('/print', ctx => {
  let data = ctx.request.body
  // let data = {"list":[{"fontstyle":"Bold","x":13.6,"y":14.8,"type":"text","value":"合格证","fontheight":4},{"fontstyle":"Normal","x":10.2,"y":24.6,"label":"品名","type":"text","value":"品名：gogogo","fontheight":3},{"fontstyle":"Normal","x":10.4,"y":32.4,"label":"款号","type":"text","value":"款号：YQ0004","fontheight":3},{"fontstyle":"Normal","sizeGroup":3,"x":12.6,"y":38.6,"label":"尺码","type":"text","value":"尺码：S","fontheight":3},{"fontstyle":"Normal","x":10.2,"y":46,"label":"颜色","type":"text","value":"颜色：黑色","fontheight":3},{"fontstyle":"Normal","x":9.6,"y":53,"label":"等级","type":"text","value":"等级：一等品","fontheight":3},{"fontstyle":"Normal","x":4.4,"y":60,"label":"执行标准","type":"text","value":"执行标准：FZ/T81004-2012","fontheight":2.9},{"fontstyle":"Normal","x":2.8,"y":67,"label":"安全类别","type":"text","value":"安全类别：GB18401-2010 B类","fontheight":2.8},{"x":16.2,"y":78,"type":"text","label":"","value":"氨纶：60%","fontstyle":"Normal","fontheight":3},{"fontstyle":"Normal","x":7.2,"y":75,"label":"面料","type":"text","value":"面料：真丝：40%","fontheight":3},{"x":5.6,"y":93.4,"type":"barcode","value":"XXX","height":6},{"fontstyle":"Bold","x":9.4,"y":85.8,"label":"售价","type":"text","value":"售价：￥150","fontheight":3.4}],"copy":1}
  // let printerName = 'TSC TTP-244 Plus'
  edgeFunc('openport')('', true)
  edgeFunc('clearbuffer')('', true)
  edgeFunc('sendcommand')('DIRECTION 1,0')
  edgeFunc('sendcommand')(`SIZE ${data.width} mm,${data.height} mm`)
  // edgeFunc('sendcommand')('GAP 3 mm')
  // edgeFunc('sendcommand')('OFFSET 30 mm')
  edgeFunc('sendcommand')('SHIFT 20 mm')
  for (let item of data.list) {
    if (['text', 'title', 'material'].includes(item.type)) {
      // console.log(item)
      edgeFunc('windowsfont')({ x: Math.round(item.x * 8), y: Math.round(item.y * 8), fontheight: Math.round(item.fontheight * 8), rotation: 0, fontstyle: fontstyle[item.fontstyle], fontunderline: 0, szFaceName: 'SimHei', content: item.value }, true)
    } else if (item.type === 'barcode') {
      edgeFunc('barcode')({ x: (item.x * 8).toFixed(), y: (item.y * 8).toFixed(), type: '128', height: (item.height * 8).toFixed(), readable: '2', rotation: '0', narrow: '2', wide: '1', code: item.value }, true)
    }
  }
  edgeFunc('printlabel')({ quantity: '1', copy: data.copy.toString() }, true)
  edgeFunc('closeport')('', true)
  ctx.body = '打印成功'
})

app.use(router.routes())

app.listen(25565)